import java.util.*;
public class PondSize {
  List<Integer> ponds = new ArrayList<>();
  public int[] pondSizes(int[][] land) {
      
      for(int i = 0; i < land.length; i++){
          for(int j = 0; j < land[0].length; j++){
              if(land[i][j] == 0){
                  int size = dfs(land, i, j);
                  ponds.add(size);
              }
          }
      }
      int[] res = new int[ponds.size()];
      for(int i = 0; i < res.length; i++){
          res[i] = ponds.get(i);
      }
      Arrays.sort(res);
      return res;
  }
  private int dfs(int[][] land, int i, int j) {
    if(i < 0 || j < 0 || i >= land.length || j >= land[0].length || land[i][j] != 0) return 0;
    land[i][j] = 1;
    int size = 1;
    size += dfs(land, i + 1, j);
    size += dfs(land, i - 1, j);
    size += dfs(land, i, j + 1);
    size += dfs(land, i, j - 1);
    size += dfs(land, i+1, j + 1);
    size += dfs(land, i+1, j - 1);
    size += dfs(land, i-1, j - 1);
    size += dfs(land, i-1, j + 1);
    return size;
  }
  public static void main(String[] args) {
    int[][] land = {{0,2,1,0}, {0,1,0,1}, {1,1,0,1}, {0,1,0,1}};
    PondSize ps = new PondSize();
    int[] res = ps.pondSizes(land);
    for(int i = 0; i < res.length; i++){
      System.out.println(res[i]);
    }
  }
}


// [0,2,1,0],
//   [0,1,0,1],
//   [1,1,0,1],
//   [0,1,0,1]